<?php
/**
 * WpValidator.php
 * 
 * This file contains the static class WpValidator 
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage base
 */


/**
 * A class to validate values
 * 
 * The WpValidator class provides functions to validate values
 * 
 * The list of valid vtypes
 * <ul>
 *  <li>not_null</li>
 *  <li>string</li>
 *  <li>string_alpha - Does not check if string is empty</li>
 *  <li>string_not_empty</li>
 *  <li>string_alphanumeric - Does not check if string is empty</li>
 *  <li>string_numeric</li>
 *  <li>number</li>
 *  <li>integer</li>
 * </ul>
 * 
 * 
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage base
 */
class WpValidator
{
	protected static function func_domain($value)
  {
    return preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $value) !== 0;
  }

	protected static function func_uuid($value)
	{
		return preg_match(
			'/^([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){8}/',
			$value) !== 0;
	}
	
	/**
	 * Validates a value with the specified validation operation
	 * @param string $vtype A valid validation type
	 * @param mixed $value The value to validate
	 * @return True if value is valid, false else
	 */
	public static function validate($vtype='not_null', $value)
	{
		switch($vtype) {
			case 'not_null': return !is_null($value);
			case 'string': return is_string($value);
			case 'string_not_empty': return (is_string($value) && $value !== '');
			case 'string_alpha': return (is_string($value) && ($value === '' || preg_match('/[A-Za-z]/', $value) !== 0));
			case 'string_alphanumeric': return (is_string($value) && ($value === '' || preg_match('/^[A-Za-z0-9]/', $value) !== 0));
			case 'number': return (is_int($value) || is_float($value));
			case 'integer': return is_int($value);
			case 'uuid': return self::func_uuid($value);
			case 'email': return filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
      case 'url': return filter_var($value, FILTER_VALIDATE_URL) !== false;
		}
	}
}
/* End of file WpValidator.php */
/* Location: ./wildpack/base/WpValidator.php */ 